<?php
namespace app\api\controller;
use think\Db;
use app\admin\model\Config;
use app\common\service\GuguService;


class ChooseSubject extends App
{
    
    //模糊搜索院校列表
    public function get_school()
    {
        
        $kw = $this->request->get("kw",'','trim'); 
        if(empty($kw)){
            return $this->show(2001,"请输入关键词!", '');
        }
		
		
		$map['college_name'] = ['like',"%".$kw."%"];
        $list = Db::name("school")->field('id,college_name')->where($map)->select();
		
		return $this->show(1,"success", $list);
		
    }
    
    //选考科目查询-院校优先
    public function school()
    {
        $pagesize = 10;
        $page = $this->request->get("page",0,'intval'); 
        
        $province_id= $this->request->get("province_id",'','trim');
        $school_type= $this->request->get("school_type",'','trim');
        $school_tese= $this->request->get("school_tese",'','trim');
        $school_xingzhi= $this->request->get("school_xingzhi",'','trim'); 
        $school_ids_1= $this->request->get("school_ids",'','trim'); 
        if(!empty($province_id)){
			$map['province_id'] = ['in',$province_id];
        }
		
        if(!empty($school_type)){
			//$school_id1 = Db::name('school_type_data')->where('type_id','in',$school_type)->column('school_id');
			$school_id1 = Db::name('school')->where('school_type','in',$school_type)->column('id');
			$map['id'] = ['in',$school_id1];
        }
        if(!empty($school_tese)){
			$school_id2 = Db::name('school_tese_data')->where('tese_id','in',$school_tese)->column('school_id');
			$map['id'] = ['in',$school_id2];
        }
        if(!empty($school_xingzhi)){
			//$school_id3 = Db::name('school_xingzhi_data')->where('xingzhi_id','in',$school_xingzhi)->column('school_id');
			$school_id3 = Db::name('school')->where('school_xingzhi','in',$school_xingzhi)->column('id');
			$map['id'] = ['in',$school_id3];
        }
		if(isset($school_id1) && isset($school_id2) && isset($school_id3)){
			$school_ids = array_intersect($school_id1,$school_id2,$school_id3);
			$map['id'] = ['in',$school_ids];
		}elseif(isset($school_id1) && isset($school_id2)){
			$school_ids = array_intersect($school_id1,$school_id2);
			$map['id'] = ['in',$school_ids];
		}elseif(isset($school_id1) && isset($school_id3)){
			$school_ids = array_intersect($school_id1,$school_id3);
			$map['id'] = ['in',$school_ids];
		}elseif(isset($school_id2) && isset($school_id3)){
			$school_ids = array_intersect($school_id2,$school_id3);
			$map['id'] = ['in',$school_ids];
		}
		
        
		//dump(implode(',', array_fill(0, count($school_xingzhi), '%')));die;
		$map['status'] = 1;
        $count = Db::name("school")->where($map)->count();
		$list = Db::name("school")->where($map)->field('id,college_name,province_id,ranking,school_type,school_xingzhi')->order('ranking asc,id desc')
			->page($page,$pagesize)->select();  
		
        if(!empty($school_ids_1)){
			$list1 = Db::name("school")->where('id','in',$school_ids_1)->field('id,college_name,province_id,ranking,school_type,school_xingzhi')->order('ranking asc,id desc')->select();  
			//$list = array_merge($list,$list1);
			if(!empty($list1)){
				$school_ids_ed = Db::name("school")->where($map)->column('id');
				foreach($list1 as $ks=>$vs){
					if(!in_array($vs['id'],$school_ids_ed)){
						array_unshift($list,$vs);
					}
				}
				
			}
        }
		//dump($list);die;
		foreach($list as $k=>$v){
			$list[$k]['province'] = Db::name('area')->where('id',$v['province_id'])->value('name');
			//$list[$k]['school_type'] = Db::name('school_type_data')->alias('d')->join('school_type t','d.type_id=t.id')->where('d.school_id',$v['id'])->value('t.title');
			$list[$k]['school_type'] = Db::name('school_type')->where('id',$v['school_type'])->value('title');
			//$list[$k]['school_xingzhi'] = Db::name('school_xingzhi_data')->alias('d')->join('school_xingzhi x','d.xingzhi_id=x.id')->where('d.school_id',$v['id'])->value('x.title');
			$list[$k]['school_xingzhi'] = Db::name('school_xingzhi')->where('id',$v['school_xingzhi'])->value('title');
			$list[$k]['school_tese'] = Db::name('school_tese_data')->alias('d')->join('school_tese t','d.tese_id=t.id')->where('d.school_id',$v['id'])->column('t.title');
			$list[$k]['zuhe'] = '';
			$list[$k]['major_nums'] = '';
		}
		
        $pages = ceil($count/$pagesize);
        $data = array('list'=>$list,'count'=>$count,'pages'=>$pages,'pagesize'=>$pagesize);	
		//dump($cate_list);die;
		return $this->show(1,"成功", $data);
		
    }
	
	public function get_school_major()
    {
        $pagesize = 100;
        $page = $this->request->get("page",1,'intval');
		
        $school_id = $this->request->get("school_id",0,'intval'); 
        if(empty($school_id)){
            return $this->show(2001,"缺少参数school_id!", '');
        }
		
		$year = date('Y',strtotime("-1 year"));
		
		
		$province_id = $this->province_id;
		$college_name = Db::name("school")->where('id',$school_id)->value('college_name');
		$enrollprovince = Db::name("area")->where('id',$province_id)->value('name');
		$result = GuguService::make('/metadata/ceemajorline',['appkey'=>'TZK6DJZ5LVS6V8LYDAS9WE3K4TM8ZQWZ','pageindex'=>$page,'pagesize'=>$pagesize,'schoolname'=>$college_name,'enrollprovince'=>$enrollprovince,'year'=>$year]);
		if($result[0] != 200){
            return $this->show(2002,$result[1]??'error', '');
		}
		$list = $result[1][1];
		
		$list_new = array_map(function ($rs){
			return ['school_name' => $rs["SchoolName"],'major_name'=>$rs['MajorName'],'subject_selection'=>$rs['SubjectSelection'],
					'batch_name'=>$rs["BatchName"]];//,'TypeName'=>$rs["type_name"]
           
        }, $list);
		return $this->show(1,"success", $list_new);
		
    }
	
	
	
	
	public function get_major_cate_one_list()
    {
        
       
		
        $education_level_list = Db::name("major")->group('education_level')->column('education_level');
        if(empty($education_level_list)){
            return $this->show(2001,"没有数据!", '');
        }
		$education_level = $education_level_list[0];
		$cate_list = Db::name("major")->where('cate_level',1)->where('education_level',$education_level)->field('id,cate_title,major_code')->order('sort asc,id asc')->select();
	
		return $this->show(1,"success", $cate_list);
		
    }
    
	public function get_major_cate_two_list()
    {
        
        $cate_id= $this->request->get("cate_id",0,'intval'); 
        if(empty($cate_id)){
            return $this->show(2001,"缺少参数cate_id!", '');
        }
		
        $education_level_list = Db::name("major")->group('education_level')->column('education_level');
        if(empty($education_level_list)){
            return $this->show(2001,"没有数据!", '');
        }
		$education_level = $education_level_list[0];
		
		$cate_list = Db::name("major")->where('cate_level',2)->where('education_level',$education_level)->where('cate_pid',$cate_id)->field('id,cate_title,major_code')->order('sort asc,id asc')->select();
		
		return $this->show(1,"success", $cate_list);
		
    }
	
	//模糊搜索专业列表
    public function get_major()
    {
        
        $kw = $this->request->get("kw",'','trim'); 
        if(empty($kw)){
            return $this->show(2001,"请输入关键词!", '');
        }
        $education_level_list = Db::name("major")->group('education_level')->column('education_level');
        if(empty($education_level_list)){
            return $this->show(2001,"没有数据!", '');
        }
		$education_level = $education_level_list[0];
		
		
		$map['cate_title'] = ['like',"%".$kw."%"];
        $list = Db::name("major")->field('id,cate_title as major_name')->where('education_level',$education_level)->where('cate_level',3)->where($map)->select();
		
		return $this->show(1,"success", $list);
		
    }
	//选考科目查询-专业优先
	public function major()
    {
        $pagesize = 100;
        $page = $this->request->get("page",1,'intval');
		
        $cate_id = $this->request->get("cate_id",0,'intval'); 
		$majorname = '';
        if(!empty($cate_id)){
			$majorname = Db::name("major")->where('id',$cate_id)->value('cate_title');
        }
		$year = date('Y',strtotime("-1 year"));
		
		$province_id = $this->province_id;
		$enrollprovince = Db::name("area")->where('id',$province_id)->value('name');
		$result = GuguService::make('/metadata/ceemajorline',['appkey'=>'TZK6DJZ5LVS6V8LYDAS9WE3K4TM8ZQWZ','pageindex'=>$page,'pagesize'=>$pagesize,'majorname'=>$majorname,'enrollprovince'=>$enrollprovince,'year'=>$year]);
		if($result[0] != 200){
            return $this->show(2002,$result[1]??'error', '');
		}
		$list = $result[1][1];
		$count = $result[1][0]['DataTotalCount'];
		
		$list_new = array_map(function ($rs){
			return ['school_name' => $rs["SchoolName"],'major_name'=>$rs['MajorName'],'subject_selection'=>$rs['SubjectSelection'],
					'batch_name'=>$rs["BatchName"]];//,'TypeName'=>$rs["type_name"]
           
        }, $list);
        $pages = ceil($count/$pagesize);
        $data = array('list'=>$list_new,'count'=>$count,'pages'=>$pages,'pagesize'=>$pagesize);	
		return $this->show(1,"success", $data);
		
    }
	//智能选科-院校优先
    public function intelligence_school()
    {
        $pagesize = 10;
        $page = $this->request->get("page",0,'intval'); 
        
        $province_id= $this->request->get("province_id",'','trim');
        $school_type= $this->request->get("school_type",'','trim');
        $school_tese= $this->request->get("school_tese",'','trim');
        $school_xingzhi= $this->request->get("school_xingzhi",'','trim'); 
        $school_ids_1= $this->request->get("school_ids",'','trim'); 
        if(!empty($province_id)){
			$map['province_id'] = ['in',$province_id];
        }
		
        if(!empty($school_type)){
			//$school_id1 = Db::name('school_type_data')->where('type_id','in',$school_type)->column('school_id');
			$school_id1 = Db::name('school')->where('school_type','in',$school_type)->column('id');
			$map['id'] = ['in',$school_id1];
        }
        if(!empty($school_tese)){
			$school_id2 = Db::name('school_tese_data')->where('tese_id','in',$school_tese)->column('school_id');
			$map['id'] = ['in',$school_id2];
        }
        if(!empty($school_xingzhi)){
			//$school_id3 = Db::name('school_xingzhi_data')->where('xingzhi_id','in',$school_xingzhi)->column('school_id');
			$school_id3 = Db::name('school')->where('school_xingzhi','in',$school_xingzhi)->column('id');
			$map['id'] = ['in',$school_id3];
        }
		if(isset($school_id1) && isset($school_id2) && isset($school_id3)){
			$school_ids = array_intersect($school_id1,$school_id2,$school_id3);
			$map['id'] = ['in',$school_ids];
		}elseif(isset($school_id1) && isset($school_id2)){
			$school_ids = array_intersect($school_id1,$school_id2);
			$map['id'] = ['in',$school_ids];
		}elseif(isset($school_id1) && isset($school_id3)){
			$school_ids = array_intersect($school_id1,$school_id3);
			$map['id'] = ['in',$school_ids];
		}elseif(isset($school_id2) && isset($school_id3)){
			$school_ids = array_intersect($school_id2,$school_id3);
			$map['id'] = ['in',$school_ids];
		}
		
        
		//dump(implode(',', array_fill(0, count($school_xingzhi), '%')));die;
		$map['status'] = 1;
        $count = Db::name("school")->where($map)->count();
		$list = Db::name("school")->where($map)->field('id,college_name,province_id,ranking,cover_image')->order('ranking asc,id desc')
			->page($page,$pagesize)->select();  
		
        if(!empty($school_ids_1)){
			$list1 = Db::name("school")->where('id','in',$school_ids_1)->field('id,college_name,province_id,ranking,cover_image,school_type,school_xingzhi')->order('ranking asc,id desc')->select();  
			//$list = array_merge($list,$list1);
			if(!empty($list1)){
				$school_ids_ed = Db::name("school")->where($map)->column('id');
				foreach($list1 as $ks=>$vs){
					if(!in_array($vs['id'],$school_ids_ed)){
						array_unshift($list,$vs);
					}
				}
				
			}
        }
		//dump($list);die;
		foreach($list as $k=>$v){
			$list[$k]['province'] = Db::name('area')->where('id',$v['province_id'])->value('name');
			//$list[$k]['school_type'] = Db::name('school_type_data')->alias('d')->join('school_type t','d.type_id=t.id')->where('d.school_id',$v['id'])->value('t.title');
			$list[$k]['school_type'] = Db::name('school_type')->where('id',$v['school_type'])->value('title');
			//$list[$k]['school_xingzhi'] = Db::name('school_xingzhi_data')->alias('d')->join('school_xingzhi x','d.xingzhi_id=x.id')->where('d.school_id',$v['id'])->value('x.title');
			$list[$k]['school_xingzhi'] = Db::name('school_xingzhi')->where('id',$v['school_xingzhi'])->value('title');
			$list[$k]['school_tese'] = Db::name('school_tese_data')->alias('d')->join('school_tese t','d.tese_id=t.id')->where('d.school_id',$v['id'])->column('t.title');
			$list[$k]['zuhe'] = '';
			$list[$k]['major_nums'] = '';
		}
		
        $pages = ceil($count/$pagesize);
        $data = array('list'=>$list,'count'=>$count,'pages'=>$pages,'pagesize'=>$pagesize);	
		//dump($cate_list);die;
		return $this->show(1,"成功", $data);
		
    }
	//智能选科-专业优先
	public function intelligence_major()
    {
        $pagesize = 10;
        $page = $this->request->get("page",1,'intval');
        $major_ids = $this->request->get("major_ids",'','trim');
		
        $cate_ids = $this->request->get("cate_ids",'','trim'); 
        if(!empty($cate_ids)){
			$map['id'] = ['in',$cate_ids];
        }
		$map['status'] = 1;
		$map['cate_level'] = 2;
		$education_level_list = Db::name("major")->group('education_level')->column('education_level');
        if(empty($education_level_list)){
            return $this->show(2001,"没有数据!", '');
        }
		$map['education_level'] = $education_level_list[0];
		
        $count = Db::name("major")->where($map)->count();
        $pages = ceil($count/$pagesize);
		$list = Db::name("major")->where($map)->field('id,disciplinary_category,cate_title,major_code')->order('major_code asc,id desc')
			->page($page,$pagesize)->select();  
		
		if(!empty($major_ids)){
			$cate_pids = Db::name("major")->where('cate_level',3)->where('education_level',$education_level_list[0])->where('id','in',$major_ids)->column('cate_pid');  
			$cate_pids = array_unique($cate_pids);//dump($cate_pids);die;
			$list1 = Db::name("major")->where('id','in',$cate_pids)->field('id,disciplinary_category,cate_title,major_code')->select();
			if(!empty($list1)){
				$major_ids_ed = Db::name("major")->where($map)->column('id');
				foreach($list1 as $ks=>$vs){
					if(!in_array($vs['id'],$major_ids_ed)){
						array_unshift($list,$vs);
					}
				}
				
			}
        }
		foreach($list as $k=>$v){
			$list[$k]['zuhe'] = '';
			$list[$k]['major_nums'] = Db::name("major")->where('cate_level',3)->where('cate_pid',$v['id'])->count();
			$list[$k]['school_nums'] = '';
		}
		
        $data = array('list'=>$list,'count'=>$count,'pages'=>$pages,'pagesize'=>$pagesize);	
		return $this->show(1,"success", $data);
		
    }
    
    //保存方案
    public function save_subject(){
        $user_id = $this->user_id;
        $province_id = $this->province_id;
        if($this->request->isPost()){
			
			$data['type'] = $this->request->post("type",1,'intval');
			if(!in_array($data['type'],[1,2])){
				return $this->show(2001,"type参数不正确!", '');
			}
			$data['school_ids'] = $this->request->post("school_ids",'','trim');
			$data['major_cate_ids'] = $this->request->post("major_cate_ids",'','trim');
			$data['school_subject_zuhe'] = $this->request->post("school_subject_zuhe",'','trim');
			$data['major_subject_zuhe'] = $this->request->post("major_subject_zuhe",'','trim');
			
			if(empty($data['school_ids']) && empty($data['major_cate_ids'])){
				return $this->show(2001,"请选择院校或专业!", '');
			}
			if(!empty($data['school_ids']) && empty($data['school_subject_zuhe'])){
				return $this->show(2001,"缺少参数school_subject_zuhe!", '');
			}
			if(!empty($data['major_cate_ids']) && empty($data['major_subject_zuhe'])){
				return $this->show(2001,"缺少参数major_subject_zuhe!", '');
			}
			$data['create_time'] = time();
			$data['user_id'] = $user_id;
			$data['province_id'] = $province_id;
			$title = Db::name("subject_fangan")->where('user_id',$user_id)->order('id desc')->value('title');
			$data['title'] = $title+1;
			$res = Db::name("subject_fangan")->insert($data);
			if( $res !== false ){
				return $this->show(1,"success", '');
			}else{
				return $this->show(2002, "fail", "");
			} 
		}else{
            return $this->show(2001, "非法请求", "");
        }
    }
}